import { Big, BigEvolution, BigState } from "../core/Big"
import Utils from "../GameUtils"
import { Player } from "../core/Player"
import { Config } from "../../Config"
import { 幽灵 } from "../evolution/幽灵"
import { 顽石 } from "../evolution/顽石"
import { 炮台 } from "../evolution/炮台"
import { 恶灵 } from "../evolution/恶灵"
import { 狙炮 } from "../evolution/狙炮"
import { 地灵 } from "../evolution/地灵"
import { 精灵 } from "../evolution/精灵"
import { 沙兵 } from "../evolution/沙兵"
import { 沙皇 } from "../evolution/沙皇"
import { 野猪 } from "../evolution/野猪"
import { 野马 } from "../evolution/野马"
import { 拳师 } from "../evolution/拳师"
import { 灵阵, 阵法 } from "../evolution/灵阵"
import { 天谕 } from "../evolution/天谕"
import { 妖术 } from "../evolution/妖术"
import { 鼓舞 } from "../evolution/鼓舞"
import { BaseHandler } from "./BaseHandler"
import { Terrain } from "../core/Point"

export class BigHandler extends BaseHandler{

    /** 大子出大 */
    bigBornBig(data:any,player:Player) {
        let cost = Config.COST_BORN_BIG
        let startPoint = this.gameMgr.map_point.get(data.start)
        let endPoint = this.gameMgr.map_point.get(data.end)
        let big_start = startPoint.big
        big_start.actionOnce()
        if (big_start.arr_states.has(BigState.FERTILE)) {
            cost--
        }
        if (big_start.arr_states.has(BigState.BURDEN)) {
            cost++
        }
        this.handlerMgr.spendAction(player, cost)
        big_start.deleteState(BigState.HIDE)
        let big_end = endPoint.big
        if(big_end != null) {
            big_end.deleteState(BigState.HIDE)
            this.gameMgr.sendBroadcast(Utils.getObj("play_effect",{"effect":"big_die","point_id":endPoint.id,"username":player.username}))
            return
        }
        if ([Terrain.SHAN,Terrain.SHUI].includes(endPoint.terrain)) {
            this.gameMgr.sendBroadcast(Utils.getObj("play_effect",{"effect":"big_die","point_id":endPoint.id,"username":player.username}))
            return
        }
        let big = new Big()
        big.init(this.gameMgr,player,endPoint,big_start.mother,big_start)
        this.gameMgr.sendBroadcast(Utils.getObj("born_big",{"big":big.toObj()}))
    }

    /** 大子移动 */
    bigMove(data:any,player:Player) {
        let cost = Config.COST_BIG_MOVE
        let startPoint = this.gameMgr.map_point.get(data.start)
        let endPoint = this.gameMgr.map_point.get(data.end)
        let big_start = startPoint.big
        big_start.actionOnce()
        if (big_start.arr_states.has(BigState.BURDEN)) {
            cost++
        }
        this.handlerMgr.spendAction(player, cost)
        let big_end = endPoint.big
        if(big_end != null) {
            big_end.deleteState(BigState.HIDE)
            big_start.die(player,endPoint.id)
            return
        }
        startPoint.big = null
        endPoint.big = big_start
        big_start.point = endPoint
        big_start.terrainAdapt()
        this.gameMgr.sendBroadcast(Utils.getObj("big_move",{"start":data.start,"end":data.end}))
        // 如果落点在九曲黄河中，自身受到一次伤害
        let mark = endPoint.getMarkNotMe(阵法.九曲黄河, big_start.player)
        if (mark != null) {
            big_start.attacked(mark.big.player)
        }
    }

    /** 大子攻击 */
    bigAttack(data:any,player:Player) {
        let cost = Config.COST_BIG_ATTACK
        let startPoint = this.gameMgr.map_point.get(data.start)
        let endPoint = this.gameMgr.map_point.get(data.end)
        let big_start = startPoint.big
        big_start.actionOnce()
        // 如果落点在地狱杀伐阵中，获得攻击消耗-2
        if (endPoint.hasMark(阵法.地狱杀伐, big_start.player)) {
            cost -= 2
        }
        // 沙地 焦渴+1
        if (big_start.arr_states.has(BigState.BURDEN)) {
            cost++
        }
        this.handlerMgr.spendAction(player, cost)
        let small = endPoint.small
        let big_end = endPoint.big
        if(small != null) {
            //小子被攻击
            small.die(player)
        } else if(big_end != null) {
            //大子被攻击
            if(big_end.attacked(player)){
                //打过了，如果有嗜血，+1行动力
                if (big_start.arr_states.has(BigState.BLOODTHIRSTY)) {
                        player.small.changeEnergy(1)
                }
                //如果有常胜，获得护盾
                if (big_start.arr_states.has(BigState.HEROIC)) {
                    big_start.addState(BigState.SHIELD)
            }
            } else {
                //攻击没打过，自己死亡
                big_start.die(player,endPoint.id)
                return
            }
        }
        //没有大子，就是移动
        startPoint.big = null
        endPoint.big = big_start
        big_start.point = endPoint
        this.gameMgr.sendBroadcast(Utils.getObj("big_attack",{"start":data.start,"end":data.end}))
        big_start.terrainAdapt()
        big_start.deleteState(BigState.HIDE)
        // 如果落点在九曲黄河中，自身受到一次伤害
        let mark = endPoint.getMarkNotMe(阵法.九曲黄河, big_start.player)
        if (mark != null) {
            big_start.attacked(mark.big.player)
        }
    }

    /** 转型 */
    evolve(data:any,player:Player) {
        let point_id = data.id
        let point = this.gameMgr.map_point.get(point_id)
        let big = point.big
        big.actionOnce()
        big.evolution = data.evolution
        this.gameMgr.sendBroadcast(Utils.getObj("evolve",{"player_username":big.player.username,"id":big.id,"evolution":big.evolution},true,false),true)
        big.sonDie(player)
        let cost = 0
        let evolution:Big = null
        switch(big.evolution) {
            case BigEvolution.幽灵: 
                cost = Config.COST_BIG_EVOLVE_幽灵
                evolution = new 幽灵(BigEvolution.幽灵); 
                break;
            case BigEvolution.恶灵: 
                cost = Config.COST_幽灵_EVOLVE_恶灵
                evolution = new 恶灵(BigEvolution.恶灵); 
                break;
            case BigEvolution.炮台:
                cost = Config.COST_BIG_EVOLVE_炮台
                evolution = new 炮台(BigEvolution.炮台); 
                break;
            case BigEvolution.狙炮:
                cost = Config.COST_炮台_EVOLVE_狙炮
                evolution = new 狙炮(BigEvolution.狙炮); 
                break;
            case BigEvolution.顽石:
                cost = Config.COST_BIG_EVOLVE_顽石
                evolution = new 顽石(BigEvolution.顽石); 
                break;
            case BigEvolution.地灵:
                cost = Config.COST_BIG_EVOLVE_地灵
                evolution = new 地灵(BigEvolution.地灵); 
                break;
            case BigEvolution.精灵:
                cost = Config.COST_BIG_EVOLVE_精灵
                evolution = new 精灵(BigEvolution.精灵); 
                break;
            case BigEvolution.沙兵:
                cost = Config.COST_BIG_EVOLVE_沙兵
                evolution = new 沙兵(BigEvolution.沙兵); 
                break;
            case BigEvolution.沙皇:
                cost = Config.COST_沙兵_EVOLVE_沙皇
                evolution = new 沙皇(BigEvolution.沙兵); 
                break;
            case BigEvolution.野猪:
                cost = Config.COST_BIG_EVOLVE_野猪
                evolution = new 野猪(BigEvolution.野猪); 
                break;
            case BigEvolution.野马:
                cost = Config.COST_BIG_EVOLVE_野马
                evolution = new 野马(BigEvolution.野马); 
                break;
            case BigEvolution.拳师:
                cost = Config.COST_BIG_EVOLVE_拳师
                evolution = new 拳师(BigEvolution.拳师); 
                break;
            case BigEvolution.灵阵:
                cost = Config.COST_BIG_EVOLVE_灵阵
                evolution = new 灵阵(BigEvolution.灵阵); 
                break;
            case BigEvolution.天谕:
                cost = Config.COST_BIG_EVOLVE_天谕
                evolution = new 天谕(BigEvolution.天谕); 
                break;
            case BigEvolution.妖术:
                cost = Config.COST_BIG_EVOLVE_妖术
                evolution = new 妖术(BigEvolution.妖术); 
                break;
            case BigEvolution.鼓舞:
                cost = Config.COST_BIG_EVOLVE_鼓舞
                evolution = new 鼓舞(BigEvolution.鼓舞); 
                break;
            default:
                console.error("有个转型体忘记写了")
        }
        evolution.evolve(big)
        this.handlerMgr.spendAction(player, cost)
    }
}